package com.ruoyi.station.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.station.domain.QueryData;
import com.ruoyi.station.domain.SCore;
import com.ruoyi.station.domain.SOpticalCable;
import com.ruoyi.station.service.SCoreService;
import com.ruoyi.station.service.SOpticalCableService;
import com.ruoyi.station.util.ParseSql;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 光缆Controller
 * @author ruoyi
 * @date 2021-08-24
 */
@RestController
@RequestMapping("/cable")
@Api(value="光缆对象操作资源", tags = "光缆对象操作资源")
public class SOpticalCableController extends BaseController
{
    @Autowired
    private SOpticalCableService sOpticalCableService;
    @Autowired
    private SCoreService sCoreService;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询光缆列表
     */
    @PreAuthorize("@ss.hasPermi('system:cable:list')")
    @PostMapping("/list")
    @ApiOperation(value="查询光缆列表", notes="查询光缆列表")
    public TableDataInfo query(@ApiParam @RequestBody Map map)
    {

        JSONArray queryData = JSONUtil.parseArray(map.get("queryData"));
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        startPage(pageNum , pageSize);
        List<String> userList = iSysUserService.selectUserByDept(SecurityUtils.getLoginUser().getUser().getDeptId());
        QueryWrapper<SOpticalCable> wrapper = new QueryWrapper<>();
        Page s = new Page<>(pageNum,pageSize);
        Page<SOpticalCable> list = sOpticalCableService.page(s,ParseSql.parse(wrapper, JSONUtil.toList(queryData, QueryData.class),userList));
        return getTableDataInfo(list.getRecords(),list.getTotal());
    }

    /**
     * 导出光缆列表
     */
    @PreAuthorize("@ss.hasPermi('system:cable:export')")
    @Log(title = "光缆", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value="导出光缆列表", notes="导出光缆列表")
    public AjaxResult export(@ApiParam SOpticalCable sOpticalCable)
    {
        QueryWrapper<SOpticalCable> wrapper = new QueryWrapper();
        List<SOpticalCable> list = sOpticalCableService.list(wrapper);
        ExcelUtil<SOpticalCable> util = new ExcelUtil<SOpticalCable>(SOpticalCable.class);
        return util.exportExcel(list, "cable");
    }

    /**
     * 获取光缆详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cable:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取光缆详细信息", notes="获取光缆详细信息")
    public AjaxResult getInfo(@ApiParam @PathVariable("id") String id)
    {
        Map resultMap = new HashMap();
        SOpticalCable sOpticalCable = sOpticalCableService.getById(id);
        List<SCore> list = sCoreService.list(new LambdaQueryWrapper<SCore>().eq(SCore::getParentId, id).orderByAsc(SCore::getCableNum));
        resultMap.put("sOpticalCable",sOpticalCable);
        resultMap.put("list",list);
        return AjaxResult.success(resultMap);
    }

    /**
     * 新增光缆
     */
    @PreAuthorize("@ss.hasPermi('system:cable:add')")
    @Log(title = "光缆", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增光缆", notes="新增光缆")
    @Transactional
    public AjaxResult add(@ApiParam @RequestBody Map map)
    {
        Map sOpticalCableMap = (Map) map.get("sOpticalCable");
        SOpticalCable sOpticalCable = BeanUtil.toBean(sOpticalCableMap, SOpticalCable.class);//光缆
        if (sOpticalCable.getId() != null ){
            sCoreService.remove(new LambdaQueryWrapper<SCore>().eq(SCore::getParentId, sOpticalCable.getId()));
        }
        sOpticalCable.setCreateBy(SecurityUtils.getUsername());
        sOpticalCableService.saveOrUpdate(sOpticalCable);
        return AjaxResult.success("保存成功");
    }

    /**
     * 修改光缆
     */
    @PreAuthorize("@ss.hasPermi('system:cable:edit')")
    @Log(title = "光缆", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改光缆", notes="修改光缆")
    public AjaxResult edit(@ApiParam @RequestBody SOpticalCable sOpticalCable)
    {
        int result = sOpticalCableService.updateById(sOpticalCable) == true ? 1 : 0;
        return toAjax(result);
    }

    /**
     * 删除光缆
     */
    @PreAuthorize("@ss.hasPermi('system:cable:remove')")
    @Log(title = "光缆", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value="删除光缆", notes="删除光缆")
    public AjaxResult remove(@ApiParam @PathVariable String[] ids)
    {
        List<String> list = Arrays.asList(ids);
        int result = sOpticalCableService.removeByIds(list) == true ? 1 : 0;
        for (String aLong : list) {
            sCoreService.remove(new LambdaQueryWrapper<SCore>().eq(SCore::getParentId , aLong));
        }
        return toAjax(result);
    }

    /**
     * 导出光缆模板
     * @return
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        return AjaxResult.success("DLBDZ_光缆.xls");
    }

    @Log(title = "导入光缆管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:cable:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        String message = sOpticalCableService.importsDistributionFrames(file);
        return AjaxResult.success(message);
    }
}
